package cn.jiyun.leyou.goods.listener;

import cn.jiyun.leyou.goods.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class ItemListener {
    @Autowired
    GoodsHtmlService goodsHtmlService;

    // 进行数据绑定
    @RabbitListener(bindings = @QueueBinding(
        // 队列
        value = @Queue(value = "com.jiyun.leyou.queue",durable = "true"),
        // 交换机的信息
        exchange =@Exchange(
                value = "com.jiyun.leyou",
                ignoreDeclarationExceptions = "true",
                type = ExchangeTypes.TOPIC
        ),
        key = {"item.insert","item.update"}
    ))
    public void saveAndEdit(Long id){
        if(id==null){
            return;
        }
        // 不管是保存还是修改，最终的操作都是生成一个新的静态页面
        goodsHtmlService.toHtml(id);
    }

    // 进行数据绑定
    @RabbitListener(bindings = @QueueBinding(
            // 队列
            value = @Queue(value = "com.jiyun.leyou.queue",durable = "true"),
            // 交换机的信息
            exchange =@Exchange(
                    value = "com.jiyun.leyou",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"item.delete"}
    ))
    public void delete(Long id){
        // 要把文件删除掉！！！
        if(id==null){
            return;
        }

        // 得到对应的id的文件对象
        File file = new File("G:\\nginx-1.14.0\\html\\"+id+".html");
        if(file.exists()){
            file.delete();
        }
    }

}
